#include "common.h"
#include "mapobject.h"
#include "geometry.h"
#include "sampleobjects.h"

#include <QtGui/QtGui>

using std::string;

/////////////////////////////////////////////////////////////////////////////////////////
/// House class implementation
/////////////////////////////////////////////////////////////////////////////////////////

House::House()
{
    houseLabel = "My Pretty House";
}

void House::drawPrimitive(QPainter *painter, const Text *text) const
{
}

void House::drawPrimitive(QPainter *painter, const Polygon *polygon) const
{
    if (polygon)
    {
        QPolygonF poly = polygon->toQPolygon();

        QBrush previous = painter->brush();
        QBrush brush;

		if ( highlighted )
			brush.setColor ( Qt::yellow );
		else
			brush.setColor ( Qt::darkYellow );

		painter->setBrush(brush);       
        painter->drawPolygon(poly);

        painter->setBrush(previous);
    }
}

void House::drawPrimitive(QPainter *painter, const Polyline *polyline) const
{
}

REGISTER_OBJECT_BY_TYPENAME ( House, "House" )


/////////////////////////////////////////////////////////////////////////////////////////
/// Road class implementation
/////////////////////////////////////////////////////////////////////////////////////////

Road::Road()
{
    roadName = "My road";
}

void Road::drawPrimitive(QPainter *painter, const Text *text) const
{
}

void Road::drawPrimitive(QPainter *painter, const Polygon *polygon) const
{
}

void Road::drawPrimitive(QPainter *painter, const Polyline *polyline) const
{
    if ( polyline )
    {
        vector<QPointF> points;
        polyline -> getPoints ( points );
        //QPolygonF poly = polyline -> toQPolygon();

        QPen previous = painter->pen();
		QPen pen ( Qt::blue );
        painter->setPen ( pen );
        for ( size_t i = 1; i < points.size(); ++i )
        {
            painter -> drawLine ( QPointF ( points[i - 1].x(), points[i - 1].y ()),
                QPointF ( points[i].x(), points[i].y ()));
        }
        painter->setPen ( previous );
    }
}

REGISTER_OBJECT_BY_TYPENAME ( Road, "Road" )


/////////////////////////////////////////////////////////////////////////////////////////
/// Route class implementation
/////////////////////////////////////////////////////////////////////////////////////////

Route::Route()
{
}

void Route::drawPrimitive(QPainter *painter, const Text *text) const
{
}

void Route::drawPrimitive(QPainter *painter, const Polygon *polygon) const
{
}

void Route::drawPrimitive(QPainter *painter, const Polyline *polyline) const
{
	vector<QPointF> points;
	polyline -> getPoints ( points );
    QPen pen ( Qt::red );
    QPen old = painter -> pen ();
    painter -> setPen ( pen );

    QBrush brush1 ( Qt::green );
    QBrush brush2 ( Qt::yellow );

    QBrush oldBrush = painter ->brush();
    const double width = 0.0001;
    const size_t last = points.size() - 1;

    painter -> setBrush ( brush1 );
    QRectF rect1 ( points[0].x() - width, points[0].y() - width, 2 * width, 2 * width );
    painter ->drawPie ( rect1, 0, 360*16 );

    painter -> setBrush ( brush2 );
    QRectF rect2 ( points[last].x() - width, points[last].y() - width, 2 * width, 2 * width );
    painter -> drawPie ( rect2, 0, 360*16 );

    //painter -> drawPie ( 
    
    for(size_t i = 1; i < points.size(); ++i)
    {
        QPointF first  ( points[i].x(), points[i].y ());
        QPointF second ( points[i - 1].x(), points[i - 1].y ());
        painter -> drawLine ( first, second );
    }
    
    painter -> setPen ( old );
    painter -> setBrush ( oldBrush );
}

REGISTER_OBJECT_BY_TYPENAME ( Route, "Route" )
